// pages/FeiSu/addMsg/addMsg.js
import encode from '../../../utils/base64.js';
import request from '../../../utils/config.js';
import util from '../../../utils/util.js';
let WXBizDataCrypt = require('../../../utils/RdWXBizDataCrypt.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEdit:false,
    SearchOper:false,
    selInfo:{},
    operList:[],
    custInfo:{},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isEdit == "Y"){
      wx.setNavigationBarTitle({
        title: '编辑客户',
      })
    }else{
      wx.setNavigationBarTitle({
        title: '新增客户',
      })
    }
    this.setData({
      isEdit: options.isEdit=="Y"?true:false,
    });
  },
  select:function(e){
    // console.log(e) 
    if (e.detail) {
      this.setData({ selInfo: JSON.parse(e.detail) })
      wx.setStorageSync("addCust_selInfo", e.detail);   
    } else {
      this.setData({ selInfo: {}})
    }
  },
  tapInput: function () {
    if (!this.data.isEdit){
      this.setData({
        SearchOper: true, 
        searchTag: [{ type: 'all'}],
        // searchValue: '',
        // tempOperList:[] //此处只有电销 可以不清空
      })
    }
  },
  getOpers:function(){
    let _this = this;
    request.requestMisApi("get","opers/noAuths", { status: '0' }, (res) => {
      if (res) {
        console.log(res)
        _this.setData({ operList: res})
      }
    }, (res) => {
      console.log("获取失败", res)
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isEdit){
      this.setData({
        custInfo: app.globalData.custInfo,
      });
    }
    this.getOpers()
    let addCust_selInfo = wx.getStorageSync("addCust_selInfo")
    if (addCust_selInfo){
      this.setData({ 
        selInfo: JSON.parse(addCust_selInfo),
        searchValue: JSON.parse(addCust_selInfo).realName
      });
    }
  },
  formSubmit: function (e) {
    console.log(app.globalData)
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let sendData = e.detail.value
    if (!sendData.custName) {
      wx.showToast({
        title: '请输入企业名称',
        icon: 'none',
      })
      return;
    }
    if (!sendData.address) {
      wx.showToast({
        title: '请输入地址',
        icon: 'none',
      })
      return;
    }
    if (sendData.mobileNo && !util.SYT_util.isValidNo(sendData.mobileNo)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
      })
      return;
    }
    
    let send={}
    if (!this.data.isEdit){
      send = {
        customInfo: {},
        saleChance: {},
        customLinkman: {},
      };
      for (let key in sendData) {
        if (sendData[key]) {
          if (key === 'custName') {
            send.customInfo[key] = sendData[key];
            send.saleChance[key] = sendData[key];
          } else if (key === 'tel') {
            send.customInfo[key] = sendData[key];
            if (sendData.mobileNo) {
              send.saleChance.tel = sendData.mobileNo;
            } else if (sendData.tel) {
              send.saleChance.tel = sendData.tel;
            }
          } else if (key === 'linkman') {
            send.customInfo[key] = sendData[key];
            send.saleChance[key] = sendData[key];
            send.customLinkman.linkman = sendData[key];
          } else if (key === 'mobileNo') {
            send.customLinkman.tel = sendData[key];
          }else if (key === 'address' || key === 'createTime' || key === 'custId' || key === 'custName' || key === 'custType' || key === 'cycleDays' || key === 'econKind' || key === 'grade' ||
            key === 'isIntroduce' || key === 'lastModifyTime' || key === 'lawsuitNum' || key === 'mobileNo' || key === 'nextTrackDate'
            || key === 'registCapi' || key === 'saleChanceId' || key === 'saleChanceNums' || key === 'status' || key === 'tel' || key === 'zone') {
            send.customInfo[key] = sendData[key];
          } else {
            send.saleChance[key] = sendData[key];
          }
        }
      }
      send.saleChance.saleOperId = app.globalData.loginUserInfo.operId;
      send.saleChance.saleOperName = app.globalData.loginUserInfo.realName;
      if (this.data.selInfo.operId) {
        send.saleChance.callOperId = this.data.selInfo.operId;
        send.saleChance.callOperName = this.data.selInfo.realName;
      }
    } else {
      for (let key in sendData) {
        if (sendData[key] && key != 'linkman' && key != 'mobileNo') {
          send[key] = sendData[key];
        }
      }
    }
    console.log(send)
    let _this = this;
    wx.showLoading({ title: ' ', mask: true })
    request.requestMisApi(this.data.isEdit ? "put" : "post",
      this.data.isEdit ? "custs/" + this.data.custInfo.custVo.custId + "/baseInfos": "custs", send, (res) => {
      console.log("添加", res)
      wx.hideLoading()
      if (res.code == '00') {
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 2000
        })
        if (!_this.data.isEdit) {
          app.globalData.listRefresh.SuSong = {
            refresh: true
          }
        }
        setTimeout(function () {
          wx.navigateBack();
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (res) => {
      console.log("获取失败", res)
    });
  },
  back: function () {
    wx.navigateBack({

    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})